﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Media;
using System.Data.OleDb;

namespace WindowsFormsApplication5
{
    public partial class MainForm : Form
    {
        private List<Bonus> listbonus = new List<Bonus>();
        private List<PictureBox> bonusDraw = new List<PictureBox>();

        StreamWriter sw;
        private int indexer =0;

        private List<Brick> listbrick = new List<Brick>();
        private List<PictureBox> listbrickDraw = new List<PictureBox>();

        private List<PictureBox> fire = new List<PictureBox>();

        private List<Water> listwater = new List<Water>();
        private List<PictureBox> listwaterDraw = new List<PictureBox>();

        private List<Forest> listforest = new List<Forest>();
        private List<PictureBox> listforestDraw = new List<PictureBox>();

        private Tank MyTank;
        private PictureBox MyTankDraw;

        private List<Rocket> rockets = new List<Rocket>();
        private List<PictureBox> rocketsDraw = new List<PictureBox>();

        private Rectangle temp, temp1;
        private PictureBox temppic;
        private List<Tank> enemys = new List<Tank>();
        private List<PictureBox> enemysDraw = new List<PictureBox>();


        private Rocket MyRocket1, temp_rocket;
        private Data data;
        private Logic logic;
        private Draw draw;
        private IntelektMove intelekt;

        private DataBase db;
        private DataBase dbwater;
        private DataBase dbforest;
        private MMedia media;
        Random rand = new Random();
        string dir = " ", fulldir = " ";
        Bitmap mybit;

        private Rectangle form;

        private const int up = 3, right = 2, left = 1, down = 4;
        private int temp_direct = 0;
       // private SoundPlayer sp;
        private byte who_fire, even, noteven = 0 ;

     //   private List<Point> trace = new List<Point>();
       

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        /// <summary>
        /// Frees the console.
        /// </summary>
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();

        private bool click = false;

      
        #region map
        int[,] coordinates = new int[571, 2];
        
        int[,] watermap = new int[48, 2];
      
        int[,] forestmap = new int[154,2] ;
       

        #endregion map

        public MainForm()
        {
           // AllocConsole();

            //cleararray(array);
            dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            fulldir = Path.Combine(dir, "2.bmp");
            mybit = new Bitmap(fulldir);
            mybit.MakeTransparent(Color.FromArgb(255, 255, 255));
            InitializeComponent();
            media = new MMedia(dir);
            draw = new Draw(this.pictureBox1);
            logic = new Logic();
            data = new Data();
           // sp = new SoundPlayer();
            form.Size = new Size(this.pictureBox1.Width, this.pictureBox1.Height);
            form.Location = new Point(this.pictureBox1.Location.X, this.pictureBox1.Location.Y);
            intelekt = new IntelektMove(dir, listbrick, listwater, form, this.pictureBox1, enemys, enemysDraw);
            //up = data.up;
            //down = data.down;
            //right = data.right;
            //left = data.left;

            pictureBox1.Visible = false;
            pictureBox3.Visible = false;
             fulldir = Path.Combine(dir, "fon1.jpg");
            
             pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
             pictureBox2.Image = Image.FromFile(fulldir);
             pictureBox2.Controls.Add(pictureBox1);
             pictureBox2.Controls.Add(button1);
             pictureBox2.Controls.Add(button2);
             pictureBox2.Controls.Add(label1);
             label1.Visible = false;
             label3.Visible = false;

             db = new DataBase(dir, data.TableMap);
             dbwater = new DataBase(dir, data.DbWater);
             dbforest = new DataBase(dir, data.DbForest);
    
        }     

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            string dir = "", fulldir = "";
            #region move
            string dir1 = " ", fulldir1 = " ";
            int index = -1;
            if (MyTank.tank != null)
            {
                int iam = enemys.IndexOf(MyTank);
                temp = new Rectangle();
                temp.Size = new Size(data.tank_width, data.tank_height);

                if (e.KeyCode == Keys.Up)
                {
                    MyTank.Direct = data.up;
                    temp.Location = new Point(MyTank.tank.Location.X, MyTank.tank.Location.Y - data.speed);

                    if (logic.Collision(MyTank.tank, form, data.up))
                    {
                        if ((logic.CheckBrick(temp, 1, 0, listbrick) < 0)
                            && (logic.CheckEnemy(temp, 1, iam, 0, enemys) < 0)
                            && (logic.CheckWater(temp, 1, 0, listwater) < 0))
                        {
                            dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            fulldir1 = Path.Combine(dir1, "mytank.bmp");

                            draw.Image(enemysDraw, fulldir1, MyTank.tank);

                            MyTank.tank = logic.Move(MyTank.tank, data.up, data.speed);
                            draw.MoveDraw(enemysDraw, MyTank.tank, 0);

                            for (int i = 0; i < enemys.Count; i++)
                            {
                                if (enemys[i].MyTank != 1)
                                {
                                    //  Console.WriteLine(" Mytank   " +enemy.MyTank);
                                    intelekt.cleararray();
                                    intelekt.way(enemys[i].tank.Location.X, enemys[i].tank.Location.Y, enemys[0].tank.Location.X, enemys[0].tank.Location.Y, ref enemys[i].trace, enemys.IndexOf(enemys[i]));


                                }
                            }

                        }
                    }
                }


                if (e.KeyCode == Keys.Left)
                {
                    MyTank.Direct = data.left;
                    temp.Location = new Point(MyTank.tank.Location.X - data.speed, MyTank.tank.Location.Y);

                    if (logic.Collision(MyTank.tank, form, data.left))
                    {

                        if ((logic.CheckBrick(temp, 1, 0, listbrick) < 0)
                            && (logic.CheckEnemy(temp, 1, iam, 0, enemys) < 0)
                            && (logic.CheckWater(temp, 1, 0, listwater) < 0))
                        {

                            dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            fulldir1 = Path.Combine(dir1, "mytank4.bmp");

                            draw.Image(enemysDraw, fulldir1, MyTank.tank);
                            MyTank.tank = logic.Move(MyTank.tank, data.left, data.speed);

                            draw.MoveDraw(enemysDraw, MyTank.tank, 0);
                            for (int i = 0; i < enemys.Count; i++)
                            {
                                if (enemys[i].MyTank != 1)
                                {
                                    //  Console.WriteLine(" Mytank   " +enemy.MyTank);
                                    intelekt.cleararray();
                                    intelekt.way(enemys[i].tank.Location.X, enemys[i].tank.Location.Y, enemys[0].tank.Location.X, enemys[0].tank.Location.Y, ref enemys[i].trace, enemys.IndexOf(enemys[i]));


                                }
                            }
                        }
                    }

                }

                if (e.KeyCode == Keys.Down)
                {
                    MyTank.Direct = data.down;
                    temp.Location = new Point(MyTank.tank.Location.X, MyTank.tank.Location.Y + data.speed);

                    if (logic.Collision(MyTank.tank, form, data.down))
                    {
                        if ((logic.CheckBrick(temp, 1, 0, listbrick) < 0)
                            && (logic.CheckEnemy(temp, 1, iam, 0, enemys) < 0)
                            && (logic.CheckWater(temp, 1, 0, listwater) < 0))
                        {
                            dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            fulldir1 = Path.Combine(dir1, "mytank3.bmp");

                            draw.Image(enemysDraw, fulldir1, MyTank.tank);
                            MyTank.tank = logic.Move(MyTank.tank, data.down, data.speed);

                            draw.MoveDraw(enemysDraw, MyTank.tank, 0);
                            for (int i = 0; i < enemys.Count; i++)
                            {
                                if (enemys[i].MyTank != 1)
                                {
                                    //  Console.WriteLine(" Mytank   " +enemy.MyTank);
                                    intelekt.cleararray();
                                    intelekt.way(enemys[i].tank.Location.X, enemys[i].tank.Location.Y, enemys[0].tank.Location.X, enemys[0].tank.Location.Y, ref enemys[i].trace, enemys.IndexOf(enemys[i]));


                                }
                            }
                        }
                    }

                }


                if (e.KeyCode == Keys.Right)
                {
                    MyTank.Direct = data.right;
                    temp.Location = new Point(MyTank.tank.Location.X + data.speed, MyTank.tank.Location.Y);

                    if (logic.Collision(MyTank.tank, form, data.right))
                    {

                        if ((logic.CheckBrick(temp, 1, 0, listbrick) < 0)
                            && (logic.CheckEnemy(temp, 1, iam, 0, enemys) < 0)
                            && (logic.CheckWater(temp, 1, 0, listwater) < 0))
                        {

                            dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            fulldir1 = Path.Combine(dir1, "mytank2.bmp");
             
                            draw.Image(enemysDraw, fulldir1, MyTank.tank);
                            MyTank.tank = logic.Move(MyTank.tank, data.right, data.speed);
                            draw.MoveDraw(enemysDraw, MyTank.tank, 0);
                            for (int i = 0; i < enemys.Count; i++)
                            {
                                if (enemys[i].MyTank != 1)
                                {
                                    //  Console.WriteLine(" Mytank   " +enemy.MyTank);
                                    intelekt.cleararray();
                                    intelekt.way(enemys[i].tank.Location.X, enemys[i].tank.Location.Y, enemys[0].tank.Location.X, enemys[0].tank.Location.Y, ref enemys[i].trace, enemys.IndexOf(enemys[i]));


                                }
                            }
                        }
                    }

                    //temp.Dispose();
                }

                index = logic.CheckBonus(MyTank.tank, listbonus);

                if (index >= 0)
                {
                    MyTank.Life += 1;
                    draw.Delete(bonusDraw, listbonus[index].RBonus);
                    listbonus.Remove(listbonus[index]);

                }

                index = -1;

            #endregion move
                #region fire
                if (e.KeyCode == Keys.Space)
                {
                    if (MyTank.Direct == data.up)
                    {
                        MyRocket1 = new Rocket(MyTank.tank.Location.X + MyTank.tank.Width / 2, MyTank.tank.Location.Y);
                        MyRocket1.direct = data.up;
                    }
                    if (MyTank.Direct == data.down)
                    {
                        MyRocket1 = new Rocket(MyTank.tank.Location.X + MyTank.tank.Width / 2, MyTank.tank.Location.Y + MyTank.tank.Height);
                        MyRocket1.direct = data.down;
                    }
                    if (MyTank.Direct == data.left)
                    {
                        MyRocket1 = new Rocket(MyTank.tank.Location.X, MyTank.tank.Location.Y + MyTank.tank.Height / 2);
                        MyRocket1.direct = data.left;
                    }
                    if (MyTank.Direct == data.right)
                    {
                        MyRocket1 = new Rocket(MyTank.tank.Location.X + MyTank.tank.Width, MyTank.tank.Location.Y + MyTank.tank.Height / 2);
                        MyRocket1.direct = data.right;
                    }
                    MyRocket1.Type = 1;
                    rockets.Add(MyRocket1);

                    dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    fulldir1 = Path.Combine(dir1, "shot.wav");

                    media.Fire(this.pictureBox1, DestroyTank, MyTank.tank);

                    draw.Add(MyRocket1.rocket, rocketsDraw, this.pictureBox1);
                    draw.Image(rocketsDraw, "", MyRocket1.rocket);

                    MyRocket1.rocket = logic.Move(MyRocket1.rocket, MyRocket1.direct, data.rocket_speed);

                    draw.MoveDraw(rocketsDraw, MyRocket1.rocket, 0);

                }

                #endregion fire
            }

        }


        private void StartGame(object sender, EventArgs e)
        {
            who_fire =  1;
            string dir1 = " "; 
            string fulldir1 = " ";
            button1.Enabled = false;
            int x = 0, y = 0;
            Random r = new Random();
          // Map();
            label3.Visible = true;
            label3.BackColor = Color.Transparent;
            
            label3.Text = "Your life ";
            
            
            pictureBox1.Visible = true;

            pictureBox1.BackColor = Color.BurlyWood;       
            
            db.ReadDB(coordinates);
            dbwater.ReadDB(watermap);
            dbforest.ReadDB(forestmap);

            for (int i = 0; i < coordinates.GetLength(0); i++)
            {
                Brick brick = new Brick(coordinates[i, 0], coordinates[i, 1]);
                listbrick.Add(brick);
                draw.Add(brick.Rect, listbrickDraw, this.pictureBox1);

               dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
               fulldir = Path.Combine(dir, "1.bmp");


               draw.Image(listbrickDraw, fulldir, brick.Rect);
   
            }

            for (int i = 0; i < forestmap.GetLength(0); i++)
            {
                Forest forest = new Forest(forestmap[i, 0], forestmap[i, 1]);
                listforest.Add(forest);
                draw.Add( forest.Rect, listforestDraw, this.pictureBox1);

                dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                fulldir = Path.Combine(dir, "forest.bmp");


                draw.Image( listforestDraw, fulldir, forest.Rect);

            }

            for (int i = 0; i < watermap.GetLength(0); i++)
            {
                Water water = new Water(watermap[i, 0], watermap[i, 1]);
                listwater.Add(water);
                draw.Add(water.Rect, listwaterDraw, this.pictureBox1);

                dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                fulldir = Path.Combine(dir, "water.bmp");


                draw.Image( listwaterDraw, fulldir, water.Rect);

            }

            MyTank = new Tank();

            dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            fulldir1 = Path.Combine(dir1, "mytank.bmp");
           // Console.WriteLine("Mytank  " + MyTank.Life);
            enemys.Add(MyTank);
            draw.Add(MyTank.tank, enemysDraw, this.pictureBox1);
            draw.Image(enemysDraw, fulldir1, MyTank.tank);
            

//#######################  generate enemys tanks
            for (int i = 0; i < data.CountEnemy; i++)
            {

                
                if ((i/2)==0)
                {
                    do
                    {
                        x = r.Next(0, 62) * 10;
                        y = r.Next(0, 52) * 10;
                    }
                    while ((logic.CheckFreePosition(x, y, listbrick, enemys, listwater)));

                    {
                        //Console.WriteLine("hello");
                        //Console.WriteLine("X " + x + "Y " + y);
                        Tank enemy = new Tank(x, y, data.life_light, data.TypeLight);
                        enemy.Type = data.light;
                        enemys.Add(enemy);

                        dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        fulldir1 = Path.Combine(dir1, "3.bmp");
                        draw.Add(enemy.tank, enemysDraw, this.pictureBox1);
                        draw.Image( enemysDraw, fulldir1, enemy.tank);

                    }
                }
               else
                {
                    
                    do
                    {
                        x = r.Next(0, 62) * 10;
                        y = r.Next(0, 52) * 10;
                    }
                    while ((logic.CheckFreePosition(x, y, listbrick, enemys, listwater)));

                    {
                     
                        Tank enemy = new Tank(x, y, data.life_hevy, data.TypeHeavy);
                        enemy.Type = data.heavy;
                        enemys.Add(enemy);

                        dir1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        fulldir1 = Path.Combine(dir1, "heavytank.bmp");
                        draw.Add( enemy.tank, enemysDraw, this.pictureBox1);
                        draw.Image(enemysDraw, fulldir1, enemy.tank);
                    }

                }
       
            }
      
          //  Console.WriteLine("Enemy  X " + enemys[0].tank.Location.X + "  Y " + enemys[0].tank.Location.Y);
            for (int i = 0; i < enemys.Count; i++ )
            {
                if (enemys[i].MyTank != 1)
                {
                    //  Console.WriteLine(" Mytank   " +enemy.MyTank);
               intelekt.cleararray();
               intelekt.way(enemys[i].tank.Location.X, enemys[i].tank.Location.Y, enemys[0].tank.Location.X, enemys[0].tank.Location.Y, ref enemys[i].trace, enemys.IndexOf(enemys[i]));

              
                }
            }

            //for (int i = 0; i < enemys.Count; i++)
            //{       
            //    if (enemys[i].MyTank != 1)
            //    {
            //        Console.WriteLine("Enemy  X " + enemys[i].tank.Location.X + "  Y " + enemys[i].tank.Location.Y + " Count " + enemys[i].trace.Count);
            //        for (int j = 0; i < enemys[i].trace.Count; i++)
            //        {
            // //           Console.WriteLine("X  " + enemy.trace[i].X + "  Y  " + enemy.trace[i].Y);
            //        }
            //    }
            //}

            shots.Start();
            EnemyMove.Start();
            EnemyFire.Start();
            BonusGenerate.Start();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
        }

        private void StartStop(object sender, EventArgs e)
        {
            if (!click)
            {
                shots.Stop();
                EnemyMove.Stop();
                EnemyFire.Stop();
                
                click = true;
                button2.Text = "Start";
            }
            else
            {
                shots.Start();
                EnemyMove.Start();
                EnemyFire.Start();
                
                click = false;
                button2.Text = "Stop";
            }
        }
        
        private void MoveShots(object sender, EventArgs e)
        {
            int temp = 0, temp1 = 0, allowindex = 0, index = 0;
            string dir = "", fulldir = "";


            if (enemys.Count > 0)
            {
                if (enemys[0].MyTank != 1)
                {
                    pictureBox1.BackColor = Color.Transparent;
                    pictureBox1.Controls.Clear();
                    dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    fulldir = Path.Combine(dir, "fon2.jpg");
                    pictureBox1.Image = Image.FromFile(fulldir);
                    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                    shots.Stop();
                    EnemyMove.Stop();
                    EnemyFire.Stop();
                    BonusGenerate.Stop();

                }

            }

            List<int> numberofrocket = new List<int>();
            #region temp
            for (int i = 0; i < (rockets.Count - 1); i++)
            {
                for (int j = i + 1; j < rockets.Count; j++)
                {

                    if (((rockets[j].rocket.Location.X >= rockets[i].rocket.Location.X)
                        && (rockets[j].rocket.Location.X <= (rockets[i].rocket.Location.X + rockets[i].rocket.Width))
                        && (rockets[j].rocket.Location.Y >= rockets[i].rocket.Location.Y)
                          && (rockets[j].rocket.Location.Y <= (rockets[i].rocket.Location.Y + rockets[i].rocket.Height)))
                     ||
                    (((rockets[i].rocket.Location.X >= rockets[j].rocket.Location.X)
                        && (rockets[i].rocket.Location.X <= (rockets[j].rocket.Location.X + rockets[j].rocket.Width))
                        && (rockets[i].rocket.Location.Y >= rockets[j].rocket.Location.Y)
                          && (rockets[i].rocket.Location.Y <= (rockets[j].rocket.Location.Y + rockets[j].rocket.Height)))))
                    {
                        numberofrocket.Add(i);
                        numberofrocket.Add(j);
                    }
                }
            }
            #endregion

            foreach (int i in numberofrocket)
            {

                draw.Delete(rocketsDraw, rockets[i - index].rocket);
                rockets.Remove(rockets[i - index]);
                index++;

            }

                for (int i = 0; i < rockets.Count; i++)
                {
                    if (rockets.Count > 0)
                    {
                        if (logic.Collision( rockets[i].rocket, form, rockets[i].direct))
                        {
                            allowindex++;
                            //logic.Move(ref rockets[i].rocket, rockets[i].direct, data.rocket_speed);

                            //draw.MoveDraw(rocketsDraw, ref rockets[i].rocket, rockets.IndexOf(rockets[i]));
                        }
                        else
                        {
                            draw.Delete( rocketsDraw, rockets[i].rocket);
                            rockets.Remove(rockets[i]);
                            i--;
                            if (i < 0) break;
                        }
                    }
                    //  Console.WriteLine("Rocket count " + Convert.ToString(rockets.Count));
                    //  Console.WriteLine("i  " + Convert.ToString(i));

                    if (rockets.Count > 0)
                    {
                        temp = logic.CheckEnemy(rockets[i].rocket, 0, -1, rockets[i].Type, enemys);
                       // Console.WriteLine("Type rocket " + rockets[i].Type);
                        if (temp >= 0)
                        {
                            //rockets[i].rocket.Dispose();
                            //enemys[temp].picbox.Dispose();
                            //Console.WriteLine("Timer Enemy " + Convert.ToString(enemys.Count));
                            if (enemys[temp].Life > 0)
                            {

                                enemys[temp].Life--;
                            }
                            else
                            {
                                media.Show(this.pictureBox1, DestroyTank, enemys[temp].tank);
                                draw.Delete(enemysDraw, enemys[temp].tank);
                                enemys.Remove(enemys[temp]);

                            }

                            draw.Delete( rocketsDraw, rockets[i].rocket);
                            rockets.Remove(rockets[i]);
                            i--;
                            //    Console.WriteLine("Timer Enemy " + Convert.ToString(enemys.Count));

                            if (i < 0) break;
                        }
                        else
                        {
                            allowindex++;
                        }
                    }


                    if (rockets.Count > 0)
                    {
                        temp1 = logic.CheckBrick( rockets[i].rocket, 0, 0, listbrick);
                        if (temp1 >= 0)
                        {
                            // Console.WriteLine("Timer Brick " + Convert.ToString(listbrick.Count));

                            draw.Delete(listbrickDraw, listbrick[temp1].Rect);
                            listbrick.Remove(listbrick[temp1]);
                            draw.Delete(rocketsDraw, rockets[i].rocket);
                            rockets.Remove(rockets[i]);
                            i--;
                            // Console.WriteLine("Timer Brick " + Convert.ToString(listbrick.Count));

                            if (i < 0) break;
                        }
                        else
                        {
                            allowindex++;
                        }
                    }
                    if (allowindex >= 3)
                    {
                        rockets[i].rocket = logic.Move( rockets[i].rocket, rockets[i].direct, data.rocket_speed);

                        draw.MoveDraw( rocketsDraw,  rockets[i].rocket, rockets.IndexOf(rockets[i]));

                        allowindex = 0;
                    }

                }
           
        }

       

   

        private void MoveEnemy(object sender, EventArgs e)
        {
            
            #region MoveEnemy
            int tempx, tempy;
            int temp_direct;
            int iam;
            //temp1 = new Rectangle();
            //temp1.Size = new Size(data.tank_width, data.tank_height);
            //dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            label3.Text = ("Your life " + Convert.ToString(MyTank.Life));
            string dir1 = " ";
            string fulldir1 = " ";
            if (indexer == 0)
            {
                
                indexer++;
            }
            temp_direct = 0;

            if (radioButton1.Checked)
            {
                intelekt.MoveRandom();
            }
            if (radioButton2.Checked)
            {
                intelekt.MoveWave(enemys, enemysDraw);
            }
            //
 
         
            #endregion MoveEnemy

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Enemy_Fire(object sender, EventArgs e)
        {
            int i=0;

            foreach (Tank enemy in enemys)
            {
                i++;
                if (who_fire == 1)
                {
                    if ((i % 2) == 0) { }
                    else continue;
                }

                    if (enemy != MyTank)
                    {
                        if (enemy.Direct == up)
                        {
                            temp_rocket = new Rocket(enemy.tank.Location.X + enemy.tank.Width / 2, enemy.tank.Location.Y);
                            temp_rocket.direct = up;
                        }
                        if (enemy.Direct == down)
                        {
                            temp_rocket = new Rocket(enemy.tank.Location.X + enemy.tank.Width / 2, enemy.tank.Location.Y + enemy.tank.Height);
                            temp_rocket.direct = down;
                        }
                        if (enemy.Direct == left)
                        {
                            temp_rocket = new Rocket(enemy.tank.Location.X, enemy.tank.Location.Y + enemy.tank.Height / 2);
                            temp_rocket.direct = left;
                        }
                        if (enemy.Direct == right)
                        {
                            temp_rocket = new Rocket(enemy.tank.Location.X + enemy.tank.Width, enemy.tank.Location.Y + enemy.tank.Height / 2);
                            temp_rocket.direct = right;
                        }
                        temp_rocket.Type = 2;
                        rockets.Add(temp_rocket);

                        draw.Add(temp_rocket.rocket, rocketsDraw, this.pictureBox1);
                        draw.Image(rocketsDraw, "", temp_rocket.rocket);
                        //logic.Move(ref temp_rocket.rocket, temp_rocket.direct, data.rocket_speed);
                        //draw.MoveDraw(ref rocketsDraw, ref temp_rocket.rocket, rockets.IndexOf(temp_rocket));

                    }
                
            }
            if (who_fire == 1) { who_fire = 2; } else { who_fire = 1; }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            media.Delete();
            DestroyTank.Stop();
        }

        private void Help_Click(object sender, EventArgs e)
        {

            if (pictureBox3.Visible == true)
            {
                pictureBox3.Visible = false;
            }
            else
            {
                pictureBox3.Visible = true;
                dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                fulldir = Path.Combine(dir, "7.png");

                pictureBox3.Image = Image.FromFile(fulldir);
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;

            }
         
        }

        private void BonusGenerate_Tick(object sender, EventArgs e)
        {
             int x, y;
             dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        
            
            for (int i = 0; i < data.CountBonus; i++)
            {

                do
                {
                    x = rand.Next(0, 62) * 10;
                    y = rand.Next(0, 52) * 10;
                }
                while ((logic.CheckFreePosition(x, y, listbrick, enemys, listwater)));

                {
                   
                    Bonus bonus = new Bonus(x, y, dir);
                    listbonus.Add(bonus);
   
                   
                    fulldir = Path.Combine(dir, bonus.BonusName1);
                    draw.Add(bonus.RBonus, bonusDraw, this.pictureBox1);
                    draw.Image(bonusDraw, fulldir, listbonus[i].RBonus);
                
                }
            }

            TimeOfActiveBonus.Start();

        }

        private void TimeOfActiveBonus_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < listbonus.Count; i++)
            {
                draw.Delete(bonusDraw, listbonus[i].RBonus);
            }
            listbonus.Clear();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            //for (int i = 0; i < array.GetLength(0); i++)
            //{
            //    for (int j = 0; j < array.GetLength(1); j++)
            //    {
            //        Console.Write(array[i, j]);
            //    }
            //    Console.WriteLine();
            //}
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
                radioButton2.Checked = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
                radioButton1.Checked = false;
        }
    }
}

